package com.nandi.emergencyinspection.ui.activity.macro

import android.content.Intent
import android.graphics.Color

import android.os.Bundle

import androidx.recyclerview.widget.LinearLayoutManager
import com.nandi.emergencyinspection.R
import com.nandi.emergencyinspection.bean.DisasterBean
import com.nandi.emergencyinspection.bean.MacroBean
import com.nandi.emergencyinspection.bean.TaskListBean
import com.nandi.emergencyinspection.mvp.macro.MacroListContract

import com.nandi.emergencyinspection.mvp.macro.MacroListPresenter
import com.nandi.emergencyinspection.ui.activity.UserInfoActivity
import com.nandi.emergencyinspection.ui.adapter.MacroListAdapter
import com.tbright.ktbaselibrary.extension.isVisiable

import com.tbright.ktbaselibrary.mvp.BaseMvpActivity
import kotlinx.android.synthetic.main.activity_main_macro.*
import kotlinx.android.synthetic.main.header_layout.*

class MainMacroActivity : BaseMvpActivity<MacroListPresenter>(), MacroListContract.MacroListView {
    private var list: MutableList<DisasterBean.RecordsBean?> = ArrayList()
    val marcoSaveAdapter = MacroListAdapter(list)
    var page = 1
    private var size = 10
    override fun getLayoutId(): Int {
        return R.layout.activity_main_macro
    }

    override fun initView(savedInstanceState: Bundle?) {
        center_txt.text = "宏观巡查"
        left_icon.isVisiable = false
        right_icon.isVisiable = true
        right_icon.setOnClickListener {
            startActivity(Intent(this, UserInfoActivity::class.java))
        }
        mPresenter?.getList(page, size)
        dataShow.apply {
            layoutManager = LinearLayoutManager(this@MainMacroActivity)
            adapter = marcoSaveAdapter
        }
        marcoSaveAdapter.setEmptyView(R.layout.empty_item)
        marcoSaveAdapter.addChildClickViewIds(R.id.input, R.id.look)
        marcoSaveAdapter.setOnItemChildClickListener { adapter, view, position ->

            when (view.id) {
                R.id.input -> {
                    val id = (adapter.data[position] as DisasterBean.RecordsBean).id
                    startActivity(
                        Intent(this, MacroActivity::class.java).putExtra(
                            "disastrerID",
                            id
                        )
                    )
                }
                R.id.look -> {
                    val id = (adapter.data[position] as DisasterBean.RecordsBean).id
                    startActivity(
                        Intent(this, MacroLookListActivity::class.java).putExtra(
                            "disastrerID",
                            id
                        )
                    )
                }
            }
        }
        initLoadMore()
        initRefreshLayout()
        marcoSaveAdapter.setList(list)
    }

    /**
     * 刷新
     */
    private fun initRefreshLayout() {
        swipe.setColorSchemeColors(Color.rgb(47, 223, 189))
        swipe.setOnRefreshListener { // 这里的作用是防止下拉刷新的时候还可以上拉加载
            marcoSaveAdapter.loadMoreModule.isEnableLoadMore = false
            // 下拉刷新，需要重置页数
            page = 1
            //请求数据
            mPresenter?.getList(page, size)
        }
    }

    /**
     * 初始化上拉加载
     */
    private fun initLoadMore() {
        marcoSaveAdapter.loadMoreModule.setOnLoadMoreListener {
            swipe.isRefreshing = false
            marcoSaveAdapter.loadMoreModule.isEnableLoadMore = true
            mPresenter?.getList(page, size)
        }
        marcoSaveAdapter.loadMoreModule.isAutoLoadMore = true
        //当自动加载开启，同时数据不满一屏时，是否继续执行自动加载更多(默认为true)
        marcoSaveAdapter.loadMoreModule.isEnableLoadMoreIfNotFullPage = false
    }

    /**
     * 设置数据到适配器
     */
    private fun setData(data: List<DisasterBean.RecordsBean>?) {
        //省略网络请求数据......
        //网络请求成功后下拉刷新与上拉加载都做如下处理:

        swipe.isRefreshing = false
        marcoSaveAdapter.loadMoreModule.isEnableLoadMore = true
        if (1 === page) {
            //如果是加载的第一页数据，用 setData()
            marcoSaveAdapter.setList(data)
        } else {
            //不是第一页，则用add
            if (data != null) {
                marcoSaveAdapter.addData(data)
            }

        }
        var dataSize = 0
        if (data != null) {
            dataSize = data.size
        }
        if (dataSize < size) {
            //如果不够一页,显示没有更多数据布局
            marcoSaveAdapter.loadMoreModule.loadMoreEnd()
        } else {
            marcoSaveAdapter.loadMoreModule.loadMoreComplete()
        }
        // page加一
        ++page
    }

    override fun showResult(result: DisasterBean?) {
        setData(result?.records)
    }
}